<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>File::Temp - return name and handle of a temporary file safely - Perldoc Browser</title>
    <link rel="search" href="https://perldoc.perl.org/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser">
    <link rel="canonical" href="https://perldoc.perl.org/File::Temp">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" />
    <style>
      body {
        background: #f4f4f5;
        color: #020202;
      }
      .navbar-dark {
        background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%);
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0);
        background-repeat: repeat-x;
      }
      .navbar-dark .navbar-nav .nav-link,
      .navbar-dark .navbar-nav .nav-link:focus { color: #fff }
      .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 }
      #wrapperlicious {
        margin: 0 auto;
        font: 0.9em 'Helvetica Neue', Helvetica, sans-serif;
        font-weight: normal;
        line-height: 1.5em;
        margin: 0;
        padding: 0;
      }
      #wrapperlicious h1 { font-size: 1.5em }
      #wrapperlicious h2 { font-size: 1.3em }
      #wrapperlicious h3 { font-size: 1.1em }
      #wrapperlicious h4 { font-size: 0.9em }
      #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3,
      #wrapperlicious h4, #wrapperlicious dt {
        color: #020202;
        margin-top: 1em;
        margin-bottom: 1em;
        position: relative;
        font-weight: bold;
      }
      #wrapperlicious a { color: inherit; text-decoration: underline }
      #wrapperlicious #toc { text-decoration: none }
      #wrapperlicious a:hover { color: #2a2a2a }
      #wrapperlicious a img { border: 0 }
      #wrapperlicious :not(pre) > code {
        color: inherit;
        background-color: rgba(0, 0, 0, 0.04);
        border-radius: 3px;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        padding: 0.3em;
      }
      #wrapperlicious dd {
        margin: 0;
        margin-left: 2em;
      }
      #wrapperlicious dt {
        color: #2a2a2a;
        font-weight: bold;
        margin-left: 0.9em;
      }
      #wrapperlicious p {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious li > p {
        margin-bottom: 0;
        margin-top: 0;
      }
      #wrapperlicious pre {
        border: 1px solid #c1c1c1;
        border-radius: 3px;
        font: 100% Consolas, Menlo, Monaco, monospace;
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious pre > code {
        display: block;
        background-color: #f6f6f6;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        line-height: 1.5em;
        text-align: left;
        white-space: pre;
        padding: 1em;
      }
      #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious ul {
        list-style-type: square;
      }
      #wrapperlicious ul ul {
        margin-bottom: 0px;
        margin-top: 0px;
      }
      #footer {
        font-size: 0.8em;
        padding-top: 0.5em;
        text-align: center;
      }
      #more {
        display: inline;
        font-size: 0.8em;
      }
      #perldocdiv {
        background-color: #fff;
        border: 1px solid #c1c1c1;
        border-bottom-left-radius: 5px;
        border-bottom-right-radius: 5px;
        margin-left: auto;
        margin-right: auto;
        padding: 3em;
        padding-top: 1em;
        max-width: 960px;
      }
      #moduleversion { float: right }
      #wrapperlicious .leading-notice {
        font-style: italic;
        padding-left: 1em;
        margin-top: 1em;
        margin-bottom: 1em;
      }
      #wrapperlicious .permalink {
        display: none;
        left: -0.75em;
        position: absolute;
        padding-right: 0.25em;
        text-decoration: none;
      }
      #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink,
      #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink,
      #wrapperlicious dt:hover .permalink {
        display: block;
      }
    </style>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'G-KVNWBNT5FB');
      gtag('config', 'UA-50555-3');
    </script>
  </head>
  <body>
    <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="https://perldoc.perl.org/">Perldoc Browser</a>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="./File::Temp.html#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.35.0</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-stable">
          <a class="dropdown-item" href="https://perldoc.perl.org/File::Temp">Latest</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.2/File::Temp">5.38.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.1/File::Temp">5.38.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0/File::Temp">5.38.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.3/File::Temp">5.36.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.2/File::Temp">5.36.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.1/File::Temp">5.36.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.0/File::Temp">5.36.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.3/File::Temp">5.34.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.2/File::Temp">5.34.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.1/File::Temp">5.34.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.0/File::Temp">5.34.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.1/File::Temp">5.32.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.0/File::Temp">5.32.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.3/File::Temp">5.30.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.2/File::Temp">5.30.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.1/File::Temp">5.30.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.0/File::Temp">5.30.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.3/File::Temp">5.28.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.2/File::Temp">5.28.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.1/File::Temp">5.28.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.0/File::Temp">5.28.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.3/File::Temp">5.26.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.2/File::Temp">5.26.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.1/File::Temp">5.26.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.0/File::Temp">5.26.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.4/File::Temp">5.24.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.3/File::Temp">5.24.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.2/File::Temp">5.24.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.1/File::Temp">5.24.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.0/File::Temp">5.24.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.4/File::Temp">5.22.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.3/File::Temp">5.22.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.2/File::Temp">5.22.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.1/File::Temp">5.22.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.0/File::Temp">5.22.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.3/File::Temp">5.20.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.2/File::Temp">5.20.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.1/File::Temp">5.20.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.0/File::Temp">5.20.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.4/File::Temp">5.18.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.3/File::Temp">5.18.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.2/File::Temp">5.18.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.1/File::Temp">5.18.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.0/File::Temp">5.18.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.3/File::Temp">5.16.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.2/File::Temp">5.16.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.1/File::Temp">5.16.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.0/File::Temp">5.16.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.4/File::Temp">5.14.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.3/File::Temp">5.14.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.2/File::Temp">5.14.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.1/File::Temp">5.14.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.0/File::Temp">5.14.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.5/File::Temp">5.12.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.4/File::Temp">5.12.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.3/File::Temp">5.12.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.2/File::Temp">5.12.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.1/File::Temp">5.12.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.0/File::Temp">5.12.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.1/File::Temp">5.10.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.0/File::Temp">5.10.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.9/File::Temp">5.8.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.8/File::Temp">5.8.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.7/File::Temp">5.8.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.6/File::Temp">5.8.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.5/File::Temp">5.8.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.4/File::Temp">5.8.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.3/File::Temp">5.8.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.2/File::Temp">5.8.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.1/File::Temp">5.8.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.0/File::Temp">5.8.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.2/File::Temp">5.6.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.1/File::Temp">5.6.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.0/File::Temp">5.6.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_04/File::Temp">5.005_04</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_03/File::Temp">5.005_03</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_02/File::Temp">5.005_02</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_01/File::Temp">5.005_01</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005/File::Temp">5.005</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="./File::Temp.html#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-dev">
          <a class="dropdown-item" href="https://perldoc.perl.org/blead/File::Temp">blead</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.6/File::Temp">5.39.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.5/File::Temp">5.39.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.4/File::Temp">5.39.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.3/File::Temp">5.39.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.2/File::Temp">5.39.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.1/File::Temp">5.39.1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC2/File::Temp">5.38.0-RC2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC1/File::Temp">5.38.0-RC1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.11/File::Temp">5.37.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.10/File::Temp">5.37.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.9/File::Temp">5.37.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.8/File::Temp">5.37.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.7/File::Temp">5.37.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.6/File::Temp">5.37.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.5/File::Temp">5.37.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.4/File::Temp">5.37.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.3/File::Temp">5.37.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.2/File::Temp">5.37.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.1/File::Temp">5.37.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.0/File::Temp">5.37.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.11/File::Temp">5.35.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.10/File::Temp">5.35.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.9/File::Temp">5.35.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.8/File::Temp">5.35.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.7/File::Temp">5.35.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.6/File::Temp">5.35.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.5/File::Temp">5.35.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.4/File::Temp">5.35.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.3/File::Temp">5.35.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.2/File::Temp">5.35.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.1/File::Temp">5.35.1</a>
          <a class="dropdown-item active" href="./File::Temp.html">5.35.0</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="./File::Temp.html#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-nav">
          <a class="dropdown-item" href="perl.html">Perl</a>
          <a class="dropdown-item" href="perlintro.html">Intro</a>
          <a class="dropdown-item" href="perl.html#Tutorials">Tutorials</a>
          <a class="dropdown-item" href="perlfaq.html">FAQs</a>
          <a class="dropdown-item" href="perl.html#Reference-Manual">Reference</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlop.html">Operators</a>
          <a class="dropdown-item" href="functions.html">Functions</a>
          <a class="dropdown-item" href="variables.html">Variables</a>
          <a class="dropdown-item" href="modules.html">Modules</a>
          <a class="dropdown-item" href="perlutil.html">Utilities</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlcommunity.html">Community</a>
          <a class="dropdown-item" href="perlhist.html">History</a>
        </div>
      </li>
    </ul>
    <ul class="navbar-nav">
      <script>
        function set_expand (expand) {
          var perldocdiv = document.getElementById('perldocdiv');
          var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width');
          var expanded = (width == '' || width == 'none') ? true : false;
          if (expand === null) {
            expand = !expanded;
          }
          if ((expand && !expanded) || (!expand && expanded)) {
            perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px');
            var button_classlist = document.getElementById('content-expand-button').classList;
            if (expand) {
              button_classlist.add('btn-light');
              button_classlist.remove('btn-outline-light');
            } else {
              button_classlist.add('btn-outline-light');
              button_classlist.remove('btn-light');
            }
          }
          return expand;
        }
        function toggle_expand () {
          var expand = set_expand(null);
          document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC';
        }
        function read_expand () {
          return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 });
        }
        if (document.readyState === 'loading') {
          document.addEventListener('DOMContentLoaded', function () {
            if (read_expand()) {
              set_expand(true);
            }
          });
        } else if (read_expand()) {
          set_expand(true);
        }
      </script>
      <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-2" onclick="toggle_expand()">Expand</button>
    <ul>
    <form class="form-inline" method="get" action="https://perldoc.perl.org/5.35.0/search">
      <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search">
    </form>
  </div>
</nav>

    <div id="wrapperlicious" class="container-fluid">
      <div id="perldocdiv">
        <div id="links">
          <a href="./File::Temp.html">File::Temp</a>
          <div id="more">
            (<a href="./File::Temp.txt">source</a>,
            <a href="https://metacpan.org/pod/File::Temp">CPAN</a>)
          </div>
            <div id="moduleversion">version 0.2311</div>
        </div>
        <div class="leading-notice">
          You are viewing the version of this documentation from Perl 5.35.0.
            This is a development version of Perl.
        </div>
        <h1><a id="toc">CONTENTS</a></h1>
                  <ul>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#NAME">NAME</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#VERSION">VERSION</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#SYNOPSIS">SYNOPSIS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#DESCRIPTION">DESCRIPTION</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#OBJECT-ORIENTED-INTERFACE">OBJECT-ORIENTED INTERFACE</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#FUNCTIONS">FUNCTIONS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#MKTEMP-FUNCTIONS">MKTEMP FUNCTIONS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#POSIX-FUNCTIONS">POSIX FUNCTIONS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#ADDITIONAL-FUNCTIONS">ADDITIONAL FUNCTIONS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#UTILITY-FUNCTIONS">UTILITY FUNCTIONS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#PACKAGE-VARIABLES">PACKAGE VARIABLES</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#WARNING">WARNING</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#Temporary-files-and-NFS">Temporary files and NFS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#Forking">Forking</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#Directory-removal">Directory removal</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#Taint-mode">Taint mode</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#BINMODE">BINMODE</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#HISTORY">HISTORY</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#SEE-ALSO">SEE ALSO</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#SUPPORT">SUPPORT</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#AUTHOR">AUTHOR</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#CONTRIBUTORS">CONTRIBUTORS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./File::Temp.html#COPYRIGHT-AND-LICENSE">COPYRIGHT AND LICENSE</a>
              </li>
          </ul>

      <h1 id="NAME"><a class="permalink" href="./File::Temp.html#NAME">#</a>NAME</h1>

<p>File::Temp - return name and handle of a temporary file safely</p>

<h1 id="VERSION"><a class="permalink" href="./File::Temp.html#VERSION">#</a>VERSION</h1>

<p>version 0.2311</p>

<h1 id="SYNOPSIS"><a class="permalink" href="./File::Temp.html#SYNOPSIS">#</a>SYNOPSIS</h1>

<pre><code>use File::Temp qw/ tempfile tempdir /;

$fh = tempfile();
($fh, $filename) = tempfile();

($fh, $filename) = tempfile( $template, DIR =&gt; $dir);
($fh, $filename) = tempfile( $template, SUFFIX =&gt; &#39;.dat&#39;);
($fh, $filename) = tempfile( $template, TMPDIR =&gt; 1 );

binmode( $fh, &quot;:utf8&quot; );

$dir = tempdir( CLEANUP =&gt; 1 );
($fh, $filename) = tempfile( DIR =&gt; $dir );</code></pre>

<p>Object interface:</p>

<pre><code>require File::Temp;
use File::Temp ();
use File::Temp qw/ :seekable /;

$fh = File::Temp-&gt;new();
$fname = $fh-&gt;filename;

$fh = File::Temp-&gt;new(TEMPLATE =&gt; $template);
$fname = $fh-&gt;filename;

$tmp = File::Temp-&gt;new( UNLINK =&gt; 0, SUFFIX =&gt; &#39;.dat&#39; );
print $tmp &quot;Some data\n&quot;;
print &quot;Filename is $tmp\n&quot;;
$tmp-&gt;seek( 0, SEEK_END );

$dir = File::Temp-&gt;newdir(); # CLEANUP =&gt; 1 by default</code></pre>

<p>The following interfaces are provided for compatibility with existing APIs. They should not be used in new code.</p>

<p>MkTemp family:</p>

<pre><code>use File::Temp qw/ :mktemp  /;

($fh, $file) = mkstemp( &quot;tmpfileXXXXX&quot; );
($fh, $file) = mkstemps( &quot;tmpfileXXXXXX&quot;, $suffix);

$tmpdir = mkdtemp( $template );

$unopened_file = mktemp( $template );</code></pre>

<p>POSIX functions:</p>

<pre><code>use File::Temp qw/ :POSIX /;

$file = tmpnam();
$fh = tmpfile();

($fh, $file) = tmpnam();</code></pre>

<p>Compatibility functions:</p>

<pre><code>$unopened_file = File::Temp::tempnam( $dir, $pfx );</code></pre>

<h1 id="DESCRIPTION"><a class="permalink" href="./File::Temp.html#DESCRIPTION">#</a>DESCRIPTION</h1>

<p><code>File::Temp</code> can be used to create and open temporary files in a safe way. There is both a function interface and an object-oriented interface. The File::Temp constructor or the tempfile() function can be used to return the name and the open filehandle of a temporary file. The tempdir() function can be used to create a temporary directory.</p>

<p>The security aspect of temporary file creation is emphasized such that a filehandle and filename are returned together. This helps guarantee that a race condition can not occur where the temporary file is created by another process between checking for the existence of the file and its opening. Additional security levels are provided to check, for example, that the sticky bit is set on world writable directories. See <a href="./File::Temp.html#safe_level">&quot;safe_level&quot;</a> for more information.</p>

<p>For compatibility with popular C library functions, Perl implementations of the mkstemp() family of functions are provided. These are, mkstemp(), mkstemps(), mkdtemp() and mktemp().</p>

<p>Additionally, implementations of the standard <a href="POSIX.html">POSIX</a> tmpnam() and tmpfile() functions are provided if required.</p>

<p>Implementations of mktemp(), tmpnam(), and tempnam() are provided, but should be used with caution since they return only a filename that was valid when function was called, so cannot guarantee that the file will not exist by the time the caller opens the filename.</p>

<p>Filehandles returned by these functions support the seekable methods.</p>

<h1 id="OBJECT-ORIENTED-INTERFACE"><a class="permalink" href="./File::Temp.html#OBJECT-ORIENTED-INTERFACE">#</a><a id="OBJECT"></a>OBJECT-ORIENTED INTERFACE</h1>

<p>This is the primary interface for interacting with <code>File::Temp</code>. Using the OO interface a temporary file can be created when the object is constructed and the file can be removed when the object is no longer required.</p>

<p>Note that there is no method to obtain the filehandle from the <code>File::Temp</code> object. The object itself acts as a filehandle. The object isa <code>IO::Handle</code> and isa <code>IO::Seekable</code> so all those methods are available.</p>

<p>Also, the object is configured such that it stringifies to the name of the temporary file and so can be compared to a filename directly. It numifies to the <code>refaddr</code> the same as other handles and so can be compared to other handles with <code>==</code>.</p>

<pre><code>$fh eq $filename       # as a string
$fh != \*STDOUT        # as a number</code></pre>

<p>Available since 0.14.</p>

<dl>

<dt id="new"><a class="permalink" href="./File::Temp.html#new">#</a><b>new</b></dt>
<dd>

<p>Create a temporary file object.</p>

<pre><code>my $tmp = File::Temp-&gt;new();</code></pre>

<p>by default the object is constructed as if <code>tempfile</code> was called without options, but with the additional behaviour that the temporary file is removed by the object destructor if UNLINK is set to true (the default).</p>

<p>Supported arguments are the same as for <code>tempfile</code>: UNLINK (defaulting to true), DIR, EXLOCK, PERMS and SUFFIX. Additionally, the filename template is specified using the TEMPLATE option. The OPEN option is not supported (the file is always opened).</p>

<pre><code>$tmp = File::Temp-&gt;new( TEMPLATE =&gt; &#39;tempXXXXX&#39;,
                       DIR =&gt; &#39;mydir&#39;,
                       SUFFIX =&gt; &#39;.dat&#39;);</code></pre>

<p>Arguments are case insensitive.</p>

<p>Can call croak() if an error occurs.</p>

<p>Available since 0.14.</p>

<p>TEMPLATE available since 0.23</p>

</dd>
<dt id="newdir"><a class="permalink" href="./File::Temp.html#newdir">#</a><b>newdir</b></dt>
<dd>

<p>Create a temporary directory using an object oriented interface.</p>

<pre><code>$dir = File::Temp-&gt;newdir();</code></pre>

<p>By default the directory is deleted when the object goes out of scope.</p>

<p>Supports the same options as the <code>tempdir</code> function. Note that directories created with this method default to CLEANUP =&gt; 1.</p>

<pre><code>$dir = File::Temp-&gt;newdir( $template, %options );</code></pre>

<p>A template may be specified either with a leading template or with a TEMPLATE argument.</p>

<p>Available since 0.19.</p>

<p>TEMPLATE available since 0.23.</p>

</dd>
<dt id="filename"><a class="permalink" href="./File::Temp.html#filename">#</a><b>filename</b></dt>
<dd>

<p>Return the name of the temporary file associated with this object (if the object was created using the &quot;new&quot; constructor).</p>

<pre><code>$filename = $tmp-&gt;filename;</code></pre>

<p>This method is called automatically when the object is used as a string.</p>

<p>Current API available since 0.14</p>

</dd>
<dt id="dirname"><a class="permalink" href="./File::Temp.html#dirname">#</a><b>dirname</b></dt>
<dd>

<p>Return the name of the temporary directory associated with this object (if the object was created using the &quot;newdir&quot; constructor).</p>

<pre><code>$dirname = $tmpdir-&gt;dirname;</code></pre>

<p>This method is called automatically when the object is used in string context.</p>

</dd>
<dt id="unlink_on_destroy"><a class="permalink" href="./File::Temp.html#unlink_on_destroy">#</a><b>unlink_on_destroy</b></dt>
<dd>

<p>Control whether the file is unlinked when the object goes out of scope. The file is removed if this value is true and $KEEP_ALL is not.</p>

<pre><code>$fh-&gt;unlink_on_destroy( 1 );</code></pre>

<p>Default is for the file to be removed.</p>

<p>Current API available since 0.15</p>

</dd>
<dt id="DESTROY"><a class="permalink" href="./File::Temp.html#DESTROY">#</a><b>DESTROY</b></dt>
<dd>

<p>When the object goes out of scope, the destructor is called. This destructor will attempt to unlink the file (using <a href="./File::Temp.html#unlink1">unlink1</a>) if the constructor was called with UNLINK set to 1 (the default state if UNLINK is not specified).</p>

<p>No error is given if the unlink fails.</p>

<p>If the object has been passed to a child process during a fork, the file will be deleted when the object goes out of scope in the parent.</p>

<p>For a temporary directory object the directory will be removed unless the CLEANUP argument was used in the constructor (and set to false) or <code>unlink_on_destroy</code> was modified after creation. Note that if a temp directory is your current directory, it cannot be removed - a warning will be given in this case. <code>chdir()</code> out of the directory before letting the object go out of scope.</p>

<p>If the global variable $KEEP_ALL is true, the file or directory will not be removed.</p>

</dd>
</dl>

<h1 id="FUNCTIONS"><a class="permalink" href="./File::Temp.html#FUNCTIONS">#</a>FUNCTIONS</h1>

<p>This section describes the recommended interface for generating temporary files and directories.</p>

<dl>

<dt id="tempfile"><a class="permalink" href="./File::Temp.html#tempfile">#</a><b>tempfile</b></dt>
<dd>

<p>This is the basic function to generate temporary files. The behaviour of the file can be changed using various options:</p>

<pre><code>$fh = tempfile();
($fh, $filename) = tempfile();</code></pre>

<p>Create a temporary file in the directory specified for temporary files, as specified by the tmpdir() function in <a href="./File::Spec.html">File::Spec</a>.</p>

<pre><code>($fh, $filename) = tempfile($template);</code></pre>

<p>Create a temporary file in the current directory using the supplied template. Trailing `X&#39; characters are replaced with random letters to generate the filename. At least four `X&#39; characters must be present at the end of the template.</p>

<pre><code>($fh, $filename) = tempfile($template, SUFFIX =&gt; $suffix)</code></pre>

<p>Same as previously, except that a suffix is added to the template after the `X&#39; translation. Useful for ensuring that a temporary filename has a particular extension when needed by other applications. But see the WARNING at the end.</p>

<pre><code>($fh, $filename) = tempfile($template, DIR =&gt; $dir);</code></pre>

<p>Translates the template as before except that a directory name is specified.</p>

<pre><code>($fh, $filename) = tempfile($template, TMPDIR =&gt; 1);</code></pre>

<p>Equivalent to specifying a DIR of &quot;File::Spec-&gt;tmpdir&quot;, writing the file into the same temporary directory as would be used if no template was specified at all.</p>

<pre><code>($fh, $filename) = tempfile($template, UNLINK =&gt; 1);</code></pre>

<p>Return the filename and filehandle as before except that the file is automatically removed when the program exits (dependent on $KEEP_ALL). Default is for the file to be removed if a file handle is requested and to be kept if the filename is requested. In a scalar context (where no filename is returned) the file is always deleted either (depending on the operating system) on exit or when it is closed (unless $KEEP_ALL is true when the temp file is created).</p>

<p>Use the object-oriented interface if fine-grained control of when a file is removed is required.</p>

<p>If the template is not specified, a template is always automatically generated. This temporary file is placed in tmpdir() (<a href="./File::Spec.html">File::Spec</a>) unless a directory is specified explicitly with the DIR option.</p>

<pre><code>$fh = tempfile( DIR =&gt; $dir );</code></pre>

<p>If called in scalar context, only the filehandle is returned and the file will automatically be deleted when closed on operating systems that support this (see the description of tmpfile() elsewhere in this document). This is the preferred mode of operation, as if you only have a filehandle, you can never create a race condition by fumbling with the filename. On systems that can not unlink an open file or can not mark a file as temporary when it is opened (for example, Windows NT uses the <code>O_TEMPORARY</code> flag) the file is marked for deletion when the program ends (equivalent to setting UNLINK to 1). The <code>UNLINK</code> flag is ignored if present.</p>

<pre><code>(undef, $filename) = tempfile($template, OPEN =&gt; 0);</code></pre>

<p>This will return the filename based on the template but will not open this file. Cannot be used in conjunction with UNLINK set to true. Default is to always open the file to protect from possible race conditions. A warning is issued if warnings are turned on. Consider using the tmpnam() and mktemp() functions described elsewhere in this document if opening the file is not required.</p>

<p>To open the temporary filehandle with O_EXLOCK (open with exclusive file lock) use <code>EXLOCK=&gt;1</code>. This is supported only by some operating systems (most notably BSD derived systems). By default EXLOCK will be false. Former <code>File::Temp</code> versions set EXLOCK to true, so to be sure to get an unlocked filehandle also with older versions, explicitly set <code>EXLOCK=&gt;0</code>.</p>

<pre><code>($fh, $filename) = tempfile($template, EXLOCK =&gt; 1);</code></pre>

<p>By default, the temp file is created with 0600 file permissions. Use <code>PERMS</code> to change this:</p>

<pre><code>($fh, $filename) = tempfile($template, PERMS =&gt; 0666);</code></pre>

<p>Options can be combined as required.</p>

<p>Will croak() if there is an error.</p>

<p>Available since 0.05.</p>

<p>UNLINK flag available since 0.10.</p>

<p>TMPDIR flag available since 0.19.</p>

<p>EXLOCK flag available since 0.19.</p>

<p>PERMS flag available since 0.2310.</p>

</dd>
<dt id="tempdir"><a class="permalink" href="./File::Temp.html#tempdir">#</a><b>tempdir</b></dt>
<dd>

<p>This is the recommended interface for creation of temporary directories. By default the directory will not be removed on exit (that is, it won&#39;t be temporary; this behaviour can not be changed because of issues with backwards compatibility). To enable removal either use the CLEANUP option which will trigger removal on program exit, or consider using the &quot;newdir&quot; method in the object interface which will allow the directory to be cleaned up when the object goes out of scope.</p>

<p>The behaviour of the function depends on the arguments:</p>

<pre><code>$tempdir = tempdir();</code></pre>

<p>Create a directory in tmpdir() (see <a href="./File::Spec.html">File::Spec</a>).</p>

<pre><code>$tempdir = tempdir( $template );</code></pre>

<p>Create a directory from the supplied template. This template is similar to that described for tempfile(). `X&#39; characters at the end of the template are replaced with random letters to construct the directory name. At least four `X&#39; characters must be in the template.</p>

<pre><code>$tempdir = tempdir ( DIR =&gt; $dir );</code></pre>

<p>Specifies the directory to use for the temporary directory. The temporary directory name is derived from an internal template.</p>

<pre><code>$tempdir = tempdir ( $template, DIR =&gt; $dir );</code></pre>

<p>Prepend the supplied directory name to the template. The template should not include parent directory specifications itself. Any parent directory specifications are removed from the template before prepending the supplied directory.</p>

<pre><code>$tempdir = tempdir ( $template, TMPDIR =&gt; 1 );</code></pre>

<p>Using the supplied template, create the temporary directory in a standard location for temporary files. Equivalent to doing</p>

<pre><code>$tempdir = tempdir ( $template, DIR =&gt; File::Spec-&gt;tmpdir);</code></pre>

<p>but shorter. Parent directory specifications are stripped from the template itself. The <code>TMPDIR</code> option is ignored if <code>DIR</code> is set explicitly. Additionally, <code>TMPDIR</code> is implied if neither a template nor a directory are supplied.</p>

<pre><code>$tempdir = tempdir( $template, CLEANUP =&gt; 1);</code></pre>

<p>Create a temporary directory using the supplied template, but attempt to remove it (and all files inside it) when the program exits. Note that an attempt will be made to remove all files from the directory even if they were not created by this module (otherwise why ask to clean it up?). The directory removal is made with the rmtree() function from the <a href="./File::Path.html">File::Path</a> module. Of course, if the template is not specified, the temporary directory will be created in tmpdir() and will also be removed at program exit.</p>

<p>Will croak() if there is an error.</p>

<p>Current API available since 0.05.</p>

</dd>
</dl>

<h1 id="MKTEMP-FUNCTIONS"><a class="permalink" href="./File::Temp.html#MKTEMP-FUNCTIONS">#</a><a id="MKTEMP"></a>MKTEMP FUNCTIONS</h1>

<p>The following functions are Perl implementations of the mktemp() family of temp file generation system calls.</p>

<dl>

<dt id="mkstemp"><a class="permalink" href="./File::Temp.html#mkstemp">#</a><b>mkstemp</b></dt>
<dd>

<p>Given a template, returns a filehandle to the temporary file and the name of the file.</p>

<pre><code>($fh, $name) = mkstemp( $template );</code></pre>

<p>In scalar context, just the filehandle is returned.</p>

<p>The template may be any filename with some number of X&#39;s appended to it, for example <i>/tmp/temp.XXXX</i>. The trailing X&#39;s are replaced with unique alphanumeric combinations.</p>

<p>Will croak() if there is an error.</p>

<p>Current API available since 0.05.</p>

</dd>
<dt id="mkstemps"><a class="permalink" href="./File::Temp.html#mkstemps">#</a><b>mkstemps</b></dt>
<dd>

<p>Similar to mkstemp(), except that an extra argument can be supplied with a suffix to be appended to the template.</p>

<pre><code>($fh, $name) = mkstemps( $template, $suffix );</code></pre>

<p>For example a template of <code>testXXXXXX</code> and suffix of <code>.dat</code> would generate a file similar to <i>testhGji_w.dat</i>.</p>

<p>Returns just the filehandle alone when called in scalar context.</p>

<p>Will croak() if there is an error.</p>

<p>Current API available since 0.05.</p>

</dd>
<dt id="mkdtemp"><a class="permalink" href="./File::Temp.html#mkdtemp">#</a><b>mkdtemp</b></dt>
<dd>

<p>Create a directory from a template. The template must end in X&#39;s that are replaced by the routine.</p>

<pre><code>$tmpdir_name = mkdtemp($template);</code></pre>

<p>Returns the name of the temporary directory created.</p>

<p>Directory must be removed by the caller.</p>

<p>Will croak() if there is an error.</p>

<p>Current API available since 0.05.</p>

</dd>
<dt id="mktemp"><a class="permalink" href="./File::Temp.html#mktemp">#</a><b>mktemp</b></dt>
<dd>

<p>Returns a valid temporary filename but does not guarantee that the file will not be opened by someone else.</p>

<pre><code>$unopened_file = mktemp($template);</code></pre>

<p>Template is the same as that required by mkstemp().</p>

<p>Will croak() if there is an error.</p>

<p>Current API available since 0.05.</p>

</dd>
</dl>

<h1 id="POSIX-FUNCTIONS"><a class="permalink" href="./File::Temp.html#POSIX-FUNCTIONS">#</a><a id="POSIX"></a>POSIX FUNCTIONS</h1>

<p>This section describes the re-implementation of the tmpnam() and tmpfile() functions described in <a href="POSIX.html">POSIX</a> using the mkstemp() from this module.</p>

<p>Unlike the <a href="POSIX.html">POSIX</a> implementations, the directory used for the temporary file is not specified in a system include file (<code>P_tmpdir</code>) but simply depends on the choice of tmpdir() returned by <a href="./File::Spec.html">File::Spec</a>. On some implementations this location can be set using the <code>TMPDIR</code> environment variable, which may not be secure. If this is a problem, simply use mkstemp() and specify a template.</p>

<dl>

<dt id="tmpnam"><a class="permalink" href="./File::Temp.html#tmpnam">#</a><b>tmpnam</b></dt>
<dd>

<p>When called in scalar context, returns the full name (including path) of a temporary file (uses mktemp()). The only check is that the file does not already exist, but there is no guarantee that that condition will continue to apply.</p>

<pre><code>$file = tmpnam();</code></pre>

<p>When called in list context, a filehandle to the open file and a filename are returned. This is achieved by calling mkstemp() after constructing a suitable template.</p>

<pre><code>($fh, $file) = tmpnam();</code></pre>

<p>If possible, this form should be used to prevent possible race conditions.</p>

<p>See <a href="./File::Spec.html#tmpdir">&quot;tmpdir&quot; in File::Spec</a> for information on the choice of temporary directory for a particular operating system.</p>

<p>Will croak() if there is an error.</p>

<p>Current API available since 0.05.</p>

</dd>
<dt id="tmpfile"><a class="permalink" href="./File::Temp.html#tmpfile">#</a><b>tmpfile</b></dt>
<dd>

<p>Returns the filehandle of a temporary file.</p>

<pre><code>$fh = tmpfile();</code></pre>

<p>The file is removed when the filehandle is closed or when the program exits. No access to the filename is provided.</p>

<p>If the temporary file can not be created undef is returned. Currently this command will probably not work when the temporary directory is on an NFS file system.</p>

<p>Will croak() if there is an error.</p>

<p>Available since 0.05.</p>

<p>Returning undef if unable to create file added in 0.12.</p>

</dd>
</dl>

<h1 id="ADDITIONAL-FUNCTIONS"><a class="permalink" href="./File::Temp.html#ADDITIONAL-FUNCTIONS">#</a><a id="ADDITIONAL"></a>ADDITIONAL FUNCTIONS</h1>

<p>These functions are provided for backwards compatibility with common tempfile generation C library functions.</p>

<p>They are not exported and must be addressed using the full package name.</p>

<dl>

<dt id="tempnam"><a class="permalink" href="./File::Temp.html#tempnam">#</a><b>tempnam</b></dt>
<dd>

<p>Return the name of a temporary file in the specified directory using a prefix. The file is guaranteed not to exist at the time the function was called, but such guarantees are good for one clock tick only. Always use the proper form of <code>sysopen</code> with <code>O_CREAT | O_EXCL</code> if you must open such a filename.</p>

<pre><code>$filename = File::Temp::tempnam( $dir, $prefix );</code></pre>

<p>Equivalent to running mktemp() with $dir/$prefixXXXXXXXX (using unix file convention as an example)</p>

<p>Because this function uses mktemp(), it can suffer from race conditions.</p>

<p>Will croak() if there is an error.</p>

<p>Current API available since 0.05.</p>

</dd>
</dl>

<h1 id="UTILITY-FUNCTIONS"><a class="permalink" href="./File::Temp.html#UTILITY-FUNCTIONS">#</a><a id="UTILITY"></a>UTILITY FUNCTIONS</h1>

<p>Useful functions for dealing with the filehandle and filename.</p>

<dl>

<dt id="unlink0"><a class="permalink" href="./File::Temp.html#unlink0">#</a><b>unlink0</b></dt>
<dd>

<p>Given an open filehandle and the associated filename, make a safe unlink. This is achieved by first checking that the filename and filehandle initially point to the same file and that the number of links to the file is 1 (all fields returned by stat() are compared). Then the filename is unlinked and the filehandle checked once again to verify that the number of links on that file is now 0. This is the closest you can come to making sure that the filename unlinked was the same as the file whose descriptor you hold.</p>

<pre><code>unlink0($fh, $path)
   or die &quot;Error unlinking file $path safely&quot;;</code></pre>

<p>Returns false on error but croaks() if there is a security anomaly. The filehandle is not closed since on some occasions this is not required.</p>

<p>On some platforms, for example Windows NT, it is not possible to unlink an open file (the file must be closed first). On those platforms, the actual unlinking is deferred until the program ends and good status is returned. A check is still performed to make sure that the filehandle and filename are pointing to the same thing (but not at the time the end block is executed since the deferred removal may not have access to the filehandle).</p>

<p>Additionally, on Windows NT not all the fields returned by stat() can be compared. For example, the <code>dev</code> and <code>rdev</code> fields seem to be different. Also, it seems that the size of the file returned by stat() does not always agree, with <code>stat(FH)</code> being more accurate than <code>stat(filename)</code>, presumably because of caching issues even when using autoflush (this is usually overcome by waiting a while after writing to the tempfile before attempting to <code>unlink0</code> it).</p>

<p>Finally, on NFS file systems the link count of the file handle does not always go to zero immediately after unlinking. Currently, this command is expected to fail on NFS disks.</p>

<p>This function is disabled if the global variable $KEEP_ALL is true and an unlink on open file is supported. If the unlink is to be deferred to the END block, the file is still registered for removal.</p>

<p>This function should not be called if you are using the object oriented interface since the it will interfere with the object destructor deleting the file.</p>

<p>Available Since 0.05.</p>

<p>If can not unlink open file, defer removal until later available since 0.06.</p>

</dd>
<dt id="cmpstat"><a class="permalink" href="./File::Temp.html#cmpstat">#</a><b>cmpstat</b></dt>
<dd>

<p>Compare <code>stat</code> of filehandle with <code>stat</code> of provided filename. This can be used to check that the filename and filehandle initially point to the same file and that the number of links to the file is 1 (all fields returned by stat() are compared).</p>

<pre><code>cmpstat($fh, $path)
   or die &quot;Error comparing handle with file&quot;;</code></pre>

<p>Returns false if the stat information differs or if the link count is greater than 1. Calls croak if there is a security anomaly.</p>

<p>On certain platforms, for example Windows, not all the fields returned by stat() can be compared. For example, the <code>dev</code> and <code>rdev</code> fields seem to be different in Windows. Also, it seems that the size of the file returned by stat() does not always agree, with <code>stat(FH)</code> being more accurate than <code>stat(filename)</code>, presumably because of caching issues even when using autoflush (this is usually overcome by waiting a while after writing to the tempfile before attempting to <code>unlink0</code> it).</p>

<p>Not exported by default.</p>

<p>Current API available since 0.14.</p>

</dd>
<dt id="unlink1"><a class="permalink" href="./File::Temp.html#unlink1">#</a><b>unlink1</b></dt>
<dd>

<p>Similar to <code>unlink0</code> except after file comparison using cmpstat, the filehandle is closed prior to attempting to unlink the file. This allows the file to be removed without using an END block, but does mean that the post-unlink comparison of the filehandle state provided by <code>unlink0</code> is not available.</p>

<pre><code>unlink1($fh, $path)
   or die &quot;Error closing and unlinking file&quot;;</code></pre>

<p>Usually called from the object destructor when using the OO interface.</p>

<p>Not exported by default.</p>

<p>This function is disabled if the global variable $KEEP_ALL is true.</p>

<p>Can call croak() if there is a security anomaly during the stat() comparison.</p>

<p>Current API available since 0.14.</p>

</dd>
<dt id="cleanup"><a class="permalink" href="./File::Temp.html#cleanup">#</a><b>cleanup</b></dt>
<dd>

<p>Calling this function will cause any temp files or temp directories that are registered for removal to be removed. This happens automatically when the process exits but can be triggered manually if the caller is sure that none of the temp files are required. This method can be registered as an Apache callback.</p>

<p>Note that if a temp directory is your current directory, it cannot be removed. <code>chdir()</code> out of the directory first before calling <code>cleanup()</code>. (For the cleanup at program exit when the CLEANUP flag is set, this happens automatically.)</p>

<p>On OSes where temp files are automatically removed when the temp file is closed, calling this function will have no effect other than to remove temporary directories (which may include temporary files).</p>

<pre><code>File::Temp::cleanup();</code></pre>

<p>Not exported by default.</p>

<p>Current API available since 0.15.</p>

</dd>
</dl>

<h1 id="PACKAGE-VARIABLES"><a class="permalink" href="./File::Temp.html#PACKAGE-VARIABLES">#</a><a id="PACKAGE"></a>PACKAGE VARIABLES</h1>

<p>These functions control the global state of the package.</p>

<dl>

<dt id="safe_level"><a class="permalink" href="./File::Temp.html#safe_level">#</a><b>safe_level</b></dt>
<dd>

<p>Controls the lengths to which the module will go to check the safety of the temporary file or directory before proceeding. Options are:</p>

<dl>

<dt id="STANDARD"><a class="permalink" href="./File::Temp.html#STANDARD">#</a>STANDARD</dt>
<dd>

<p>Do the basic security measures to ensure the directory exists and is writable, that temporary files are opened only if they do not already exist, and that possible race conditions are avoided. Finally the <a href="./File::Temp.html#unlink0">unlink0</a> function is used to remove files safely.</p>

</dd>
<dt id="MEDIUM"><a class="permalink" href="./File::Temp.html#MEDIUM">#</a>MEDIUM</dt>
<dd>

<p>In addition to the STANDARD security, the output directory is checked to make sure that it is owned either by root or the user running the program. If the directory is writable by group or by other, it is then checked to make sure that the sticky bit is set.</p>

<p>Will not work on platforms that do not support the <code>-k</code> test for sticky bit.</p>

</dd>
<dt id="HIGH"><a class="permalink" href="./File::Temp.html#HIGH">#</a>HIGH</dt>
<dd>

<p>In addition to the MEDIUM security checks, also check for the possibility of ``chown() giveaway&#39;&#39; using the <a href="POSIX.html">POSIX</a> sysconf() function. If this is a possibility, each directory in the path is checked in turn for safeness, recursively walking back to the root directory.</p>

<p>For platforms that do not support the <a href="POSIX.html">POSIX</a> <code>_PC_CHOWN_RESTRICTED</code> symbol (for example, Windows NT) it is assumed that ``chown() giveaway&#39;&#39; is possible and the recursive test is performed.</p>

</dd>
</dl>

<p>The level can be changed as follows:</p>

<pre><code>File::Temp-&gt;safe_level( File::Temp::HIGH );</code></pre>

<p>The level constants are not exported by the module.</p>

<p>Currently, you must be running at least perl v5.6.0 in order to run with MEDIUM or HIGH security. This is simply because the safety tests use functions from <a href="Fcntl.html">Fcntl</a> that are not available in older versions of perl. The problem is that the version number for Fcntl is the same in perl 5.6.0 and in 5.005_03 even though they are different versions.</p>

<p>On systems that do not support the HIGH or MEDIUM safety levels (for example Win NT or OS/2) any attempt to change the level will be ignored. The decision to ignore rather than raise an exception allows portable programs to be written with high security in mind for the systems that can support this without those programs failing on systems where the extra tests are irrelevant.</p>

<p>If you really need to see whether the change has been accepted simply examine the return value of <code>safe_level</code>.</p>

<pre><code>$newlevel = File::Temp-&gt;safe_level( File::Temp::HIGH );
die &quot;Could not change to high security&quot;
    if $newlevel != File::Temp::HIGH;</code></pre>

<p>Available since 0.05.</p>

</dd>
<dt id="TopSystemUID"><a class="permalink" href="./File::Temp.html#TopSystemUID">#</a>TopSystemUID</dt>
<dd>

<p>This is the highest UID on the current system that refers to a root UID. This is used to make sure that the temporary directory is owned by a system UID (<code>root</code>, <code>bin</code>, <code>sys</code> etc) rather than simply by root.</p>

<p>This is required since on many unix systems <code>/tmp</code> is not owned by root.</p>

<p>Default is to assume that any UID less than or equal to 10 is a root UID.</p>

<pre><code>File::Temp-&gt;top_system_uid(10);
my $topid = File::Temp-&gt;top_system_uid;</code></pre>

<p>This value can be adjusted to reduce security checking if required. The value is only relevant when <code>safe_level</code> is set to MEDIUM or higher.</p>

<p>Available since 0.05.</p>

</dd>
<dt id="$KEEP_ALL"><a class="permalink" href="./File::Temp.html#$KEEP_ALL">#</a><a id="KEEP_ALL"></a><b>$KEEP_ALL</b></dt>
<dd>

<p>Controls whether temporary files and directories should be retained regardless of any instructions in the program to remove them automatically. This is useful for debugging but should not be used in production code.</p>

<pre><code>$File::Temp::KEEP_ALL = 1;</code></pre>

<p>Default is for files to be removed as requested by the caller.</p>

<p>In some cases, files will only be retained if this variable is true when the file is created. This means that you can not create a temporary file, set this variable and expect the temp file to still be around when the program exits.</p>

</dd>
<dt id="$DEBUG"><a class="permalink" href="./File::Temp.html#$DEBUG">#</a><a id="DEBUG"></a><b>$DEBUG</b></dt>
<dd>

<p>Controls whether debugging messages should be enabled.</p>

<pre><code>$File::Temp::DEBUG = 1;</code></pre>

<p>Default is for debugging mode to be disabled.</p>

<p>Available since 0.15.</p>

</dd>
</dl>

<h1 id="WARNING"><a class="permalink" href="./File::Temp.html#WARNING">#</a>WARNING</h1>

<p>For maximum security, endeavour always to avoid ever looking at, touching, or even imputing the existence of the filename. You do not know that that filename is connected to the same file as the handle you have, and attempts to check this can only trigger more race conditions. It&#39;s far more secure to use the filehandle alone and dispense with the filename altogether.</p>

<p>If you need to pass the handle to something that expects a filename then on a unix system you can use <code>&quot;/dev/fd/&quot; . fileno($fh)</code> for arbitrary programs. Perl code that uses the 2-argument version of <code>open</code> can be passed <code>&quot;+&lt;=&amp;&quot; . fileno($fh)</code>. Otherwise you will need to pass the filename. You will have to clear the close-on-exec bit on that file descriptor before passing it to another process.</p>

<pre><code>use Fcntl qw/F_SETFD F_GETFD/;
fcntl($tmpfh, F_SETFD, 0)
    or die &quot;Can&#39;t clear close-on-exec flag on temp fh: $!\n&quot;;</code></pre>

<h2 id="Temporary-files-and-NFS"><a class="permalink" href="./File::Temp.html#Temporary-files-and-NFS">#</a><a id="Temporary"></a>Temporary files and NFS</h2>

<p>Some problems are associated with using temporary files that reside on NFS file systems and it is recommended that a local filesystem is used whenever possible. Some of the security tests will most probably fail when the temp file is not local. Additionally, be aware that the performance of I/O operations over NFS will not be as good as for a local disk.</p>

<h2 id="Forking"><a class="permalink" href="./File::Temp.html#Forking">#</a>Forking</h2>

<p>In some cases files created by File::Temp are removed from within an END block. Since END blocks are triggered when a child process exits (unless <code>POSIX::_exit()</code> is used by the child) File::Temp takes care to only remove those temp files created by a particular process ID. This means that a child will not attempt to remove temp files created by the parent process.</p>

<p>If you are forking many processes in parallel that are all creating temporary files, you may need to reset the random number seed using srand(EXPR) in each child else all the children will attempt to walk through the same set of random file names and may well cause themselves to give up if they exceed the number of retry attempts.</p>

<h2 id="Directory-removal"><a class="permalink" href="./File::Temp.html#Directory-removal">#</a><a id="Directory"></a>Directory removal</h2>

<p>Note that if you have chdir&#39;ed into the temporary directory and it is subsequently cleaned up (either in the END block or as part of object destruction), then you will get a warning from File::Path::rmtree().</p>

<h2 id="Taint-mode"><a class="permalink" href="./File::Temp.html#Taint-mode">#</a><a id="Taint"></a>Taint mode</h2>

<p>If you need to run code under taint mode, updating to the latest <a href="./File::Spec.html">File::Spec</a> is highly recommended. On Windows, if the directory given by <a href="https://perldoc.perl.org/5.35.0/File::Spec::tmpdir">File::Spec::tmpdir</a> isn&#39;t writable, File::Temp will attempt to fallback to the user&#39;s local application data directory or croak with an error.</p>

<h2 id="BINMODE"><a class="permalink" href="./File::Temp.html#BINMODE">#</a>BINMODE</h2>

<p>The file returned by File::Temp will have been opened in binary mode if such a mode is available. If that is not correct, use the <code>binmode()</code> function to change the mode of the filehandle.</p>

<p>Note that you can modify the encoding of a file opened by File::Temp also by using <code>binmode()</code>.</p>

<h1 id="HISTORY"><a class="permalink" href="./File::Temp.html#HISTORY">#</a>HISTORY</h1>

<p>Originally began life in May 1999 as an XS interface to the system mkstemp() function. In March 2000, the OpenBSD mkstemp() code was translated to Perl for total control of the code&#39;s security checking, to ensure the presence of the function regardless of operating system and to help with portability. The module was shipped as a standard part of perl from v5.6.1.</p>

<p>Thanks to Tom Christiansen for suggesting that this module should be written and providing ideas for code improvements and security enhancements.</p>

<h1 id="SEE-ALSO"><a class="permalink" href="./File::Temp.html#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1>

<p><a href="POSIX.html#tmpnam">&quot;tmpnam&quot; in POSIX</a>, <a href="POSIX.html#tmpfile">&quot;tmpfile&quot; in POSIX</a>, <a href="./File::Spec.html">File::Spec</a>, <a href="./File::Path.html">File::Path</a></p>

<p>See <a href="./IO::File.html">IO::File</a> and <a href="./File::MkTemp.html">File::MkTemp</a>, <a href="./Apache::TempFile.html">Apache::TempFile</a> for different implementations of temporary file handling.</p>

<p>See <a href="./File::Tempdir.html">File::Tempdir</a> for an alternative object-oriented wrapper for the <code>tempdir</code> function.</p>

<h1 id="SUPPORT"><a class="permalink" href="./File::Temp.html#SUPPORT">#</a>SUPPORT</h1>

<p>Bugs may be submitted through <a href="https://rt.cpan.org/Public/Dist/Display.html?Name=File-Temp">the RT bug tracker</a> (or <a href="mailto:bug-File-Temp@rt.cpan.org">bug-File-Temp@rt.cpan.org</a>).</p>

<p>There is also a mailing list available for users of this distribution, at <a href="http://lists.perl.org/list/cpan-workers.html">http://lists.perl.org/list/cpan-workers.html</a>.</p>

<p>There is also an irc channel available for users of this distribution, at <a href="irc://irc.perl.org/#toolchain"><code>#toolchain</code> on <code>irc.perl.org</code></a>.</p>

<h1 id="AUTHOR"><a class="permalink" href="./File::Temp.html#AUTHOR">#</a>AUTHOR</h1>

<p>Tim Jenness &lt;tjenness@cpan.org&gt;</p>

<h1 id="CONTRIBUTORS"><a class="permalink" href="./File::Temp.html#CONTRIBUTORS">#</a>CONTRIBUTORS</h1>

<ul>

<li><p>Tim Jenness &lt;t.jenness@jach.hawaii.edu&gt;</p>

</li>
<li><p>Karen Etheridge &lt;ether@cpan.org&gt;</p>

</li>
<li><p>David Golden &lt;dagolden@cpan.org&gt;</p>

</li>
<li><p>Slaven Rezic &lt;srezic@cpan.org&gt;</p>

</li>
<li><p>mohawk2 &lt;mohawk2@users.noreply.github.com&gt;</p>

</li>
<li><p>Roy Ivy III &lt;rivy.dev@gmail.com&gt;</p>

</li>
<li><p>Peter Rabbitson &lt;ribasushi@cpan.org&gt;</p>

</li>
<li><p>Olivier Mengué &lt;dolmen@cpan.org&gt;</p>

</li>
<li><p>Peter John Acklam &lt;pjacklam@online.no&gt;</p>

</li>
<li><p>Tim Gim Yee &lt;tim.gim.yee@gmail.com&gt;</p>

</li>
<li><p>Nicolas R &lt;atoomic@cpan.org&gt;</p>

</li>
<li><p>Brian Mowrey &lt;brian@drlabs.org&gt;</p>

</li>
<li><p>Dagfinn Ilmari Mannsåker &lt;ilmari@ilmari.org&gt;</p>

</li>
<li><p>David Steinbrunner &lt;dsteinbrunner@pobox.com&gt;</p>

</li>
<li><p>Ed Avis &lt;eda@linux01.wcl.local&gt;</p>

</li>
<li><p>Guillem Jover &lt;guillem@hadrons.org&gt;</p>

</li>
<li><p>James E. Keenan &lt;jkeen@verizon.net&gt;</p>

</li>
<li><p>Kevin Ryde &lt;user42@zip.com.au&gt;</p>

</li>
<li><p>Ben Tilly &lt;btilly@gmail.com&gt;</p>

</li>
</ul>

<h1 id="COPYRIGHT-AND-LICENSE"><a class="permalink" href="./File::Temp.html#COPYRIGHT-AND-LICENSE">#</a><a id="COPYRIGHT"></a>COPYRIGHT AND LICENSE</h1>

<p>This software is copyright (c) 2020 by Tim Jenness and the UK Particle Physics and Astronomy Research Council.</p>

<p>This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.</p>


      </div>
      <div id="footer">
        <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p>

<p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p>


      </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    <script src="../highlight.pack.js"></script>
    <script>hljs.highlightAll();</script>
  </body>
</html>
